$(function () {
    initPage()
});

var columns = [
    {
        "data": function (row, type, val, meta) {
            return '<input type="checkbox" name="adminId" value="' + row.adminId + '">'
        }
    },
    {"data": "adminId"},
    {"data": "adminName"},
    {"data": "phone"},
    {"data": "email"},
    {
        "data": function (row, type, val, meta) {
            return row.sex == "1" ? "男" : row.sex == "0" ? "女" : ""
        }
    },
    {
        "data": function (row, type, val, meta) {
            return login2Date(row.addTime.time);
        }
    },
    {
        "data": function (row, type, val, meta) {
            return row.status == "1" ? '<span class="label label-success radius">已启用</span>' : '<span class="label radius">已停用</span>'
        }
    },
    {
        "data": function (row, type, val, meta) {
            var htmlStr = '';
            if (row.status == "1") {
                htmlStr = '<a onClick="admin_stop(this,' + row.adminId + ')" href="javascript:;" title="停用" style="text-decoration:none"><i class="Hui-iconfont">&#xe631;</i></a>';
            } else {
                htmlStr = '<a style="text-decoration:none" onClick="admin_start(this,' + row.adminId + ')" href="javascript:;" title="启用"><i class="Hui-iconfont">&#xe615;</i></a>';
            }
            htmlStr += '<a title="编辑" href="javascript:;" onclick="admin_edit(\'管理员编辑\',\'/admin/admin-edit.html?adminId=' + row.adminId + '\',\'800\',\'500\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>' +
                '<a title="删除" href="javascript:;" onclick="admin_del(this,' + row.adminId + ')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>'
            return htmlStr;
        }
    },
]

function initPage() {
    var data = httpClients({
        _url: "/admin/adminList"
    });
    console.log(data);
    if (data.code == "1") {
        initDataTable(columns, data.data)
    } else {
        layer.msg(data.message, {icon: 1, time: 1000});
    }
}

/*管理员-增加/编辑*/
function admin_edit(title, url, w, h) {
    /*
	参数解释：
	title	标题
	url		请求的url
	w		弹出层宽度（缺省调默认值）
	h		弹出层高度（缺省调默认值）
    */
    layer_show(title, url, w, h);
}

/*管理员-删除*/
function admin_del(obj, id) {
    layer.confirm('确认要删除吗？', function () {
        if (httpClients({
            _url: "/admin/adminUpStatus",
            _type: "POST",
            _data: {
                adminId: id,
                status: "0"
            }
        }).code == "1") {
            $(obj).parents("tr").remove();
            layer.msg('已删除!', {icon: 1, time: 1000});
        }

    });
}

/*管理员-停用*/
function admin_stop(obj, id) {
    layer.confirm('确认要停用吗？', function () {
        if (httpClients({
            _url: "/admin/adminUpStatus",
            _type: "POST",
            _data: {
                adminId: id,
                status: "2"
            }
        }).code == "1") {
            $(obj).parents("td").prepend('<a onClick="admin_start(this,\'' + id + '\')" href="javascript:;" title="启用" style="text-decoration:none"><i class="Hui-iconfont">&#xe615;</i></a>');
            $(obj).parents("td").prev().html('<span class="label label-default radius">已禁用</span>');
            $(obj).remove();
            layer.msg('已停用!', {icon: 5, time: 1000});
        }
    });
}

/*管理员-启用*/
function admin_start(obj, id) {
    layer.confirm('确认要启用吗？', function () {
        if (httpClients({
            _url: "/admin/adminUpStatus",
            _type: "POST",
            _data: {
                adminId: id,
                status: "1"
            }
        }).code == "1") {
            $(obj).parents("td").prepend('<a onClick="admin_stop(this,\'' + id + '\')" href="javascript:;" title="停用" style="text-decoration:none"><i class="Hui-iconfont">&#xe631;</i></a>');
            $(obj).parents("td").prev().html('<span class="label label-success radius">已启用</span>');
            $(obj).remove();
            layer.msg('已启用!', {icon: 6, time: 1000});
        }
    });
}

function datadel() {
    layer.confirm('确认要删除吗？', function () {
        var arr = []
        $('input[name=adminId]:checkbox:checked').each(function(){
            arr.push($(this).val());
        });
        if (httpClients({
            _url: "/admin/adminDel",
            _type: "POST",
            _data: {
                adminIds: arr.join(",")
            }
        }).code == "1") {
            $('input[name=adminId]:checkbox:checked').each(function(){
                $(this).parents("tr").remove();
            });
            layer.msg('已删除!', {icon: 1, time: 1000});
        }
    });
}